<template>
  <div style="padding: 5px;">
    <el-row :gutter='10'>
      <el-col :xs="12" :sm="8" :md="6" :lg="4" v-for='(item, index) in filesList' :key="item.id">
        <div class="square">
          <div class="content">
            <label :for="'subscribefiles' + item.id" class="file-checkbox">
              <div :style="{ 'background-color': selectedOptionsObj[item.id] ? '#67C23A' : '#CDD0D6' }">
                <el-icon v-show="selectedOptionsObj[item.id]"><Select /></el-icon>
                <el-icon v-show="!selectedOptionsObj[item.id]">
                  <Close />
                </el-icon>
              </div>
            </label>
            <input type="checkbox" v-model="selectedOptions" :value="item.id" :id="'subscribefiles' + item.id"
              style="display:none"> </input>
            <FileItem :item="item" @picture-card-preview="pictureCardPreviewHandle(index)" />
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
  <el-dialog v-model="dialogVisible" fullscreen align-center style="text-align:center;" :close-on-click-modal='false'
    :append-to-body="true">
    <div class='dialog-body '>
      <el-carousel style="height: 100%;" height="100%" motion-blur :autoplay="false" @change="previewFile">
        <el-carousel-item :initial-index="initialIndex" v-for="(item, index) in filesList" :key="item">
          <el-image style="width: 100%; height: 100%;" :src="dialogImageUrl" fit="contain" />
        </el-carousel-item>
      </el-carousel>

    </div>
    <!--  -->

  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, watch, computed } from 'vue';
import FileItem from './file-item.vue'
import { createThumbnail, createImageUrl } from '@/utils/image.js'

import useFilesStore from '@/stores/files.js';
const filesStore = useFilesStore();
const {getDirFiles}=filesStore;

const props = defineProps({
  dirId: {
    type: Number,
    default: 0
  },
  page: {
    type: Number,
    default: 1
  },

});
const filesList = computed(() => {
  return filesStore.files.find(item => {
    return item.dirId === props.dirId*1 && item.page === props.page
  })?.list
});

const initialIndex = ref(0);//预览图序列
const dialogImageUrl = ref('');//缩略图
const dialogVisible = ref(false);//是否显示缩略图
const selectedOptions = defineModel({
  default: function () {
    return new Array();
  }
});
//多选项
const selectedOptionsObj = computed(() => {
  const obj = {}
  selectedOptions.value.forEach(item => {
    obj[item] = true
  })
  return obj
})

//点击图片信息
 function pictureCardPreviewHandle (index) {
  dialogVisible.value = true;
  previewFile(index)
}
//预览图片
function previewFile(index) {
  const url = createImageUrl(filesList.value[index]);
  dialogImageUrl.value = url;
  initialIndex.value = index;
}
//轮播图切换
const chageCarousel = function (index) {

}//

//取得选中文件
function getSelected (){
  const i=selectedOptions.value[0]
  return 
}
defineExpose({
	getSelected
})
watch(()=>props.dirId, (newQuestion, oldQuestion) => {
  selectedOptions.value.length=0
	getDirFiles(newQuestion, { is_count: 1, page: props.page });
});

getDirFiles(props.dirId, { is_count: 1, page: props.page }); 
</script>
<style scoped>
.dialog-body {
  background-color: green;
  margin: calc(-10px - var(--el-dialog-padding-primary)) calc(4px - var(--el-dialog-padding-primary));
  height: calc(100vh - var(--el-dialog-padding-primary) - 16px);
}

.el-carousel__container {
  height: 100%;
}

.square {
  width: 100%;
  position: relative;
}

.square:after {
  content: "";
  display: block;
  padding-bottom: 100%;
}

.content {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 1px dashed #b7b5b5;
  border-radius: 4%;
}

.file-checkbox {
  position: absolute;
  left: 10px;
  top: 10px;
  font-size: 2em;
  z-index: 2;
  line-height: 1em;
}
</style>